//
//  HMWelcomeViewController.swift
//  Weibo
//
//  Created by 吴少飞 on 16/9/22.
//  Copyright © 2016年 wushaofei. All rights reserved.
//

import UIKit
import SDWebImage
class HMWelcomeViewController: UIViewController {

    //背景图片
    private lazy var backgroundImageView:UIImageView = UIImageView(image:UIImage(named: "ad_background"))
    
    //头像
    private lazy var userImageView : UIImageView = {
        
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        if let imageUrl = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large{
            
            
         view.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        view.layer.cornerRadius = view.bounds.size.width / 2
        view.layer.masksToBounds = true
        return view
        
    }()
    
    //文本信息
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel()
        
        if let name = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount?.name{
            
            label.text = "欢迎回来,\(name)"
        }else{
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        label.alpha = 0
        return label
    }()
    
    override func loadView() {
        
        view = backgroundImageView
    }
    
     override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        
            }

    
    private func setupUI() {
        
        
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        userImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 85, height: 85))
            
        }
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
        }
        
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    
    private func startAnimation(){
        
        userImageView.snp_updateConstraints { (make) in
            
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animateKeyframes(withDuration: 1, delay: 0, options: [], animations: { 
            
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
                UIView.animate(withDuration: 2, animations: {
                    
                    self.messageLabel.alpha = 1
                },completion: { (_)in
                    
                  
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: SwitchRootVCNotification), object: nil)
                    
                })
        }
        
    }
}










